//
//  JGProgressHUDHelper.m
//  XTCloud
//
//  Created by QA on 2017/12/8.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "JGProgressHUDHelper.h"
#import "JGProgressHUDFadeZoomAnimation.h"
#import "JGProgressHUDSuccessIndicatorView.h"
#import "JGProgressHUDErrorIndicatorView.h"
#import "MainAppDelegate.h"

@implementation JGProgressHUDHelper


#pragma mark -
#pragma mark JGProgressHUD 普通型

+ (JGProgressHUD *)showProgressHUDText:(NSString*)text
{
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.tag = 1;
    
    HUD.textLabel.text = @"成功";
    HUD.indicatorView = [[JGProgressHUDSuccessIndicatorView alloc] init];
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    HUD.square = YES;
    
    // 延迟0.5秒执行：
    MainAppDelegate *mDelegate = (MainAppDelegate*)[UIApplication sharedApplication].delegate;
    [HUD showInView:mDelegate.navigationController.view];
    [HUD dismissAfterDelay:1.0];
    
    return HUD;
}

#pragma mark -
#pragma mark JGProgressHUD 定制型

+ (JGProgressHUD *)showSimpleHUDText:(NSString*)text {
    
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.textLabel.text = @"加载中...";
    HUD.tag = 0;
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    
    MainAppDelegate *mDelegate = (MainAppDelegate*)[UIApplication sharedApplication].delegate;
    [HUD showInView:mDelegate.navigationController.view];
    //[HUD dismissAfterDelay:3.0];
    return HUD;
}

+ (JGProgressHUD *)showErrorHUDText:(NSString*)text
{
    return [self showErrorHUDText:text dismissAfterDelay:0.0f];
}

+ (JGProgressHUD *)showSuccessHUDText:(NSString*)text
{
    return [self showSuccessHUDText:text dismissAfterDelay:0.0f];
}

+ (JGProgressHUD *)showSuccessHUDText:(NSString*)text dismissAfterDelay:(CGFloat)delay
{
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.tag = 1;
    
    HUD.textLabel.text = @"成功";
    HUD.indicatorView = [[JGProgressHUDSuccessIndicatorView alloc] init];
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    HUD.square = YES;
    
    // 延迟0.5秒执行：
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        dispatch_async(dispatch_get_main_queue(), ^{
            MainAppDelegate *mDelegate = (MainAppDelegate*)[UIApplication sharedApplication].delegate;
            [HUD showInView:mDelegate.navigationController.view];
            [HUD dismissAfterDelay:1.0];
        });
    });
    
    return HUD;
}

+ (JGProgressHUD *)showErrorHUDText:(NSString*)text  dismissAfterDelay:(CGFloat)delay
{
    JGProgressHUD *HUD = [JGProgressHUD progressHUDWithStyle:JGProgressHUDStyleDark];
    HUD.tag = 2;
    
    HUD.textLabel.text = @"失败";
    HUD.indicatorView = [[JGProgressHUDErrorIndicatorView alloc] init];
    
    if(text && text.length>0)
        HUD.textLabel.text = text;
    
    JGProgressHUDFadeZoomAnimation *an = [JGProgressHUDFadeZoomAnimation animation];
    HUD.animation = an;
    
    HUD.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    HUD.HUDView.layer.shadowColor = [UIColor blackColor].CGColor;
    HUD.HUDView.layer.shadowOffset = CGSizeZero;
    HUD.HUDView.layer.shadowOpacity = 0.4f;
    HUD.HUDView.layer.shadowRadius = 8.0f;
    HUD.square = YES;
    
    // 延迟0.5秒执行：
    double delayInSeconds = delay;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        dispatch_async(dispatch_get_main_queue(), ^{
            MainAppDelegate *mDelegate = (MainAppDelegate*)[UIApplication sharedApplication].delegate;
            [HUD showInView:mDelegate.navigationController.view];
            [HUD dismissAfterDelay:1.0];
        });
    });
    
    return HUD;
}

@end
